package com.example.miaosh.util;

import java.io.Serializable;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;

import lombok.Data;
import lombok.Getter;

/**
 * ResultCode 统一Result返回
 * 
 * @author kai
 *
 */
@Data
public class ResultUnify implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Getter
	public enum ResultCode {

		/**
		 * 成功
		 */
		SUCCESS(200, "success"),

		/**
		 * 失败
		 */
		FAILURE(400, "fialure"),

		/**
		 * 资源未找到
		 */
		NOTFOUND(404, "not found"),

		/**
		 * 参数错误
		 */
		EINVAL(415, "缺少参数"),

		/**
		 * 服务器异常
		 */
		EXCEPTION(500, "server exception");

		/**
		 * 响应码
		 */
		private Integer errorCode;

		/**
		 * 响应信息
		 */
		private String message;

		ResultCode(Integer errorCode, String message) {
			this.errorCode = errorCode;
			this.message = message;
		}
	}

	/**
	 * 状态码
	 */
	protected Integer errorCode;

	/**
	 * 消息
	 */
	private String message;

	/**
	 * 数据对象
	 */
	@JsonInclude(value = Include.NON_NULL)
	private Object data;

	/**
	 * 成功
	 * 
	 * @param result
	 * @return
	 */
	public ResultUnify success() {
		this.errorCode = ResultCode.SUCCESS.getErrorCode();
		this.message = ResultCode.SUCCESS.getMessage();
		this.data = null;
		return this;
	}

	/**
	 * 成功
	 * 
	 * @param result
	 * @return
	 */
	public ResultUnify success(Object data) {
		this.errorCode = ResultCode.SUCCESS.getErrorCode();
		this.message = ResultCode.SUCCESS.getMessage();
		this.data = data;
		return this;
	}

	/**
	 * 400查询失败
	 * 
	 * @return
	 */
	public ResultUnify error400() {
		this.errorCode = ResultCode.FAILURE.getErrorCode();
		this.message = ResultCode.FAILURE.getMessage();
		this.data = null;
		return this;
	}

	/**
	 * 400查询失败
	 * 
	 * @return
	 */
	public ResultUnify error400(Object data) {
		this.errorCode = ResultCode.FAILURE.getErrorCode();
		this.message = ResultCode.FAILURE.getMessage();
		this.data = data;
		return this;
	}

	/**
	 * 404未找到资源
	 * 
	 * @return
	 */
	public ResultUnify error404() {
		this.errorCode = ResultCode.NOTFOUND.getErrorCode();
		this.message = ResultCode.NOTFOUND.getMessage();
		this.data = null;
		return this;
	}

	/**
	 * 415参数错误
	 * 
	 * @return
	 */
	public ResultUnify error415() {
		this.errorCode = ResultCode.EINVAL.getErrorCode();
		this.message = ResultCode.EINVAL.getMessage();
		this.data = null;
		return this;
	}

	/**
	 * 500服务器异常
	 * 
	 * @return
	 */
	public ResultUnify error500() {
		this.errorCode = ResultCode.EXCEPTION.getErrorCode();
		this.message = ResultCode.EXCEPTION.getMessage();
		this.data = null;
		return this;
	}

	/**
	 * 自定义返回值
	 * 
	 * @return
	 */
	public ResultUnify custom(Object data, Integer errorCode, String message) {
		this.errorCode = errorCode;
		this.message = message;
		this.data = data;
		return this;
	}

}
